<?php

/**
 * Run Cornjob Tasks
 *
 * @author Syed Ekramuddin Emon
 */
class AutorunCommand extends CConsoleCommand {

    public function getHelp() {
        echo "Running Profit Transaction Cornjob";
    }

    public function run() {
        $curdate = date("Y-m-d");
        $list = Yii::app()->db->createCommand("select * from member_position where substr(expire_date,1,10) >='" . $curdate . "'")->queryAll();
        //var_dump($list);
        foreach ($list as $position) {
            $sql = "select * from member_transaction_history where
					   substr(transaction_date,1,10) = '" . $curdate . "' and member_id=" . $position['member_id'] .
                    " and positions=" . $position["member_positions_id"];
            $tHistory = Yii::app()->db->createCommand($sql)->queryAll();

            //var_dump($tHistory);
            if (!empty($tHistory)) {
                Yii::log("Transaction on current date for that positons:" . $position["member_positions_id"] . " already exists", 'error', 'application');
                continue;
            }

            $sql = "select * from member_transaction_history where member_transaction_history_id = (
                    select max( member_transaction_history_id ) from member_transaction_history 
                    where member_id =" . $position['member_id'] . ")";

            $_lh = Yii::app()->db->createCommand($sql)->queryAll();
            //var_dump($_lh);
            $balance = 0;
            if (!empty($_lh)) {
                $_history = $_lh[0];
                if (!empty($_history)) {
                    $balance = $_history['balance'];
                }
            }
            if (empty($balance)) {
                $balance = 0;
            } 
            $mthModel = new MemberTransactionHistory();
            $mthModel->member_id = $position["member_id"];
            $mthModel->credit = $position["no_of_positions"] * 10 * 2 / 100;
            $mthModel->debit = 0;
            $mthModel->transaction_date = date("Y-m-d H:i:s");
            $mthModel->balance = $balance + $mthModel->credit;
            $mthModel->particulars = "Added profit amount for positions id :" . $position["member_positions_id"] . " and number of positions :" . $position["no_of_positions"];
            $mthModel->positions = $position["member_positions_id"];
//            echo "\n-------------------\n";
//            echo "balance:".$mthModel->balance."---";
//            echo "credit:".$mthModel->credit."---";
//            echo "transaction_date:".$mthModel->transaction_date."---";
//            echo "particulars:".$mthModel->particulars."---";
//            echo "positions:".$mthModel->positions."---";
            //$mthModel->save();
            if (!$mthModel->save()) {
                $msg = print_r($mthModel->getErrors(), 1);
                Yii::log("Error ".$msg, 'info', 'application');    
                //echo $msg;
            }
            //echo "model_id:".$mthModel->member_transaction_history_id."---";
            //echo $mthModel->member_id;
            Yii::log("Member transaction history added for " + $position["member_id"], 'info', 'application');
        }
    }

}

?>
